package com.league.blog.controller;

import com.league.blog.mapper.ArticleMapper;
import com.league.blog.mapper.UserMapper;
import com.league.blog.pojo.ArticleWithBLOBs;
import com.league.blog.pojo.User;
import com.league.blog.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 黄豪琦
 * 日期:2020-04-23 14:02
 * 说明:
 */
@Controller
@Slf4j
@RequestMapping("/p")
public class DickController {

    private UserMapper userMapper;

    private ArticleMapper articleMapper;

    @RequestMapping("/{accountName}")
    public String toUserPage(@PathVariable("accountName")String accountName, Model model){
        if(!StringUtils.isBlank(accountName) && accountName != null){
            if(userMapper.doesItExist(accountName) > 0){
                final User dbUser = userMapper.selectUserByAccountName(accountName);
                model.addAttribute("dbUser", dbUser);

                final List<ArticleWithBLOBs> articleList =
                        articleMapper.selectArticleListByUserAccount(dbUser.getAccountName());
                model.addAttribute("articleList", articleList);

                return "user";
            } else {
                return "common/404";
            }
        }
        return "user";
    }

    @RequestMapping("/getCurrentUser")
    public String getCurrentUser(Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user != null){
            model.addAttribute("user", user);
        }
        return "module/navigation::navigation";
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setArticleMapper(ArticleMapper articleMapper) {
        this.articleMapper = articleMapper;
    }
}
